﻿using System;
using System.Collections.Generic;
using System.Web;

namespace NsfcSurvey.Web.Helper
{
    public class UserHelper
    {

        public static void StoreSession(string deptCode, string deptName, string username, string phone, string email, int? category)
        {
            UserSessionInfo userInfo = new UserSessionInfo();
            userInfo.DepartmentCode = deptCode;
            userInfo.DepartmentName = deptName;
            userInfo.Username = username;
            userInfo.Phone = phone;
            userInfo.Email = email;
            userInfo.Categorty = category;

            HttpContext.Current.Session["user"] = userInfo;
        }

        public static void ClearSession()
        {
            HttpContext.Current.Session["user"] = null;
        }

        public static bool CheckSession()
        {
            return HttpContext.Current.Session["user"] != null;
        }

        public static string GetDepartmentName()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).DepartmentName;
            }
            else
            {
                ReLogin();
                return string.Empty;
            }
        }

        public static string GetDepartmentCode()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).DepartmentCode;
            }
            else
            {
                ReLogin();
                return string.Empty;
            }
        }

        public static string GetUserName()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).Username;
            }
            else
            {
                ReLogin();
                return string.Empty;
            }
        }

        public static string GetPhone()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).Phone;
            }
            else
            {
                ReLogin();
                return string.Empty;
            }
        }

        public static string GetEmail()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).Email;
            }
            else
            {
                ReLogin();
                return string.Empty;
            }
        }

        public static int? GetCategory1()
        {
            if (CheckSession())
            {
                return ((UserSessionInfo)(HttpContext.Current.Session["user"])).Categorty;
            }
            else
            {
                ReLogin();
                return null;
            }
        }

        private static void ReLogin()
        {
            HttpContext.Current.Response.Redirect("~/Index.aspx");
        }
    }

    public class UserSessionInfo
    {
        private string departmentName;

        public string DepartmentName
        {
            get { return departmentName; }
            set { departmentName = value; }
        }
        private string departmentCode;

        public string DepartmentCode
        {
            get { return departmentCode; }
            set { departmentCode = value; }
        }

        private int? categorty;

        public int? Categorty
        {
            get { return categorty; }
            set { categorty = value; }
        }

        private string username;
        private string phone;
        private string email;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public string Phone
        {
            get { return phone; }
            set { phone = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
    }
}